package com.clip.dao;
// default package

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.clip.entity.AlertUser;

@Repository
public class AlertUserDAO extends BasicDao  {
    private static final Log log = LogFactory.getLog(AlertUserDAO.class);
	//property constants
	public static final String UID = "uid";
	public static final String EVENTID = "eventid";
	public static final String USEREMAILID = "useremailid";
	public static final String CONTACTID = "contactid";



    
    public void save(AlertUser transientInstance) {
        log.debug("saving AlertUser instance");
        try {
            getSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(AlertUser persistentInstance) {
        log.debug("deleting AlertUser instance");
        try {
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
	public void deleteAllByEventid(String eventid) {
        try {
            getSession().createQuery("delete from AlertUser au where au.eventid='"+eventid+"'").executeUpdate();
        } catch (RuntimeException re) {
            throw re;
        }
    }
	
    public AlertUser findById( java.lang.Integer id) {
        log.debug("getting AlertUser instance with id: " + id);
        try {
            AlertUser instance = (AlertUser) getSession()
                    .get(AlertUser.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(AlertUser instance) {
        log.debug("finding AlertUser instance by example");
        try {
            List results = getSession()
                    .createCriteria(AlertUser.class)
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding AlertUser instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from AlertUser as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByUid(Object uid
	) {
		return findByProperty(UID, uid
		);
	}
	
	public List findByEventid(Object eventid
	) {
		return findByProperty(EVENTID, eventid
		);
	}
	
	public List findByUseremailid(Object useremailid
	) {
		return findByProperty(USEREMAILID, useremailid
		);
	}
	
	public List findByContactid(Object contactid
	) {
		return findByProperty(CONTACTID, contactid
		);
	}
	

	public List findAll() {
		log.debug("finding all AlertUser instances");
		try {
			String queryString = "from AlertUser";
	         Query queryObject = getSession().createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public AlertUser merge(AlertUser detachedInstance) {
        log.debug("merging AlertUser instance");
        try {
            AlertUser result = (AlertUser) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(AlertUser instance) {
        log.debug("attaching dirty AlertUser instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(AlertUser instance) {
        log.debug("attaching clean AlertUser instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void excuteUpdateQuery(String query){
    	try {
			getSession().createQuery(query).executeUpdate();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public List findByQuery(String query){
		return getSession().createQuery(query).list();
    }
    
    public Object countByQuery(String query){
    	return getSession().createQuery(query).uniqueResult();
    }
}